package com.zhoug.fileselector3demo;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.zhoug.common3.utils.IntentUtils;
import com.zhoug.fileselector3.beans.LocalMedia;
import com.zhoug.fileselector3.looker.FilePreview;
import com.zhoug.fileselector3.selector.FileSelector;
import com.zhoug.fileselector3.selector.FileSelectorBuilder;
import com.zhoug.fileselector3.selector.FileSelectorDialog;
import com.zhoug.fileselector3.selector.IFileSelector;
import com.zhoug.common3.permission.ZPermissions;

import java.util.List;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;


public class MainActivity extends MyBaseActivity {
    private IFileSelector mIFileSelector;
    private FileSelectorDialog mFileSelectorDialog;

    private Button mBtn1;
    private TextView mTvResult;
    private FilePreview mFilePreview;
    private Button mBtn2;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {

        mBtn1 = findViewById(R.id.btn1);
        mTvResult = findViewById(R.id.tv_result);
        mFilePreview = findViewById(R.id.filePreview);
        mBtn2 = findViewById(R.id.btn2);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        mFilePreview.setDeleteEnable(true);
        FileSelectorBuilder builder = new FileSelectorBuilder(this);
        mIFileSelector = builder.captureAll(true)
                .compressImage(true)
                .singleModel(false)
                .setMaxNum(5)
                .setOnSelectListener((FileSelector.onSelectListener) this::onSelectFileResult)
                .build();

        mBtn1.setOnClickListener(v -> {
            ZPermissions.with(this)
                    .setPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .setCallback((success, granted, denied) -> {
                        mIFileSelector.show();
                    })
                    .request();

        });

         mFileSelectorDialog=new FileSelectorDialog(this);
        mFileSelectorDialog.setOnSelectListener((FileSelector.onSelectListener) this::onSelectFileResult);
        mBtn2.setOnClickListener(v->{
            mFileSelectorDialog.show();

        });
    }

    private void onSelectFileResult(List<LocalMedia> medias){
        if (medias != null && !medias.isEmpty()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (LocalMedia item : medias) {
                stringBuilder.append(item.getPath());
                stringBuilder.append("\n");
            }
            mTvResult.setText(stringBuilder.toString());
            mFilePreview.setFiles(medias);
        }else{
            mTvResult.setText(null);
            mFilePreview.setFiles(null);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mIFileSelector != null) {
            mIFileSelector.onActivityResult(requestCode, resultCode, data);
        }
        if (mFileSelectorDialog != null) {
            mFileSelectorDialog.onActivityResult(requestCode, resultCode, data);
        }
    }
}